<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\web\controller\Express;
use think\Db;
use think\Validate;

/**
 * 钟房预订控制器
 * @package app\index\controller
 */
class RoomBook extends Auth
{
    public $noNeedLogin = [];

    /**
     *获取订房列表
     * @return array
     */
    public function lists()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $getData = $this->request->post();
            $sql = Db::name('qt_room_book')
                ->alias('b')
                ->leftJoin('qt_room r','r.id = b.room_id')
                ->leftJoin('qt_users u','u.id = b.sale_id')
                ->field('b.*,r.name,u.real_name as create_name,u.group,r.book_room')
                ->where(['b.store_id' => $userInfo['store_id'],'b.status'=>'1']);
            if(isset($getData['room_id']) && !empty($getData['room_id'])){
                $sql->where('b.room_id',$getData['room_id']);
            }
            $data = $sql->order('b.id desc')->paginate($getData['limit'])->each(function ($item){
                $item['sale_name'] = Db::name('qt_users')->where(['id'=>$item['sale_id']])->value('real_name');
                return $item;
            });
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 添加订房
     */
    public function add(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            $validate = new Validate();
            if (!$validate->scene('add')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            $roomStatus = Db::name('qt_room_book')->where(['room_id'=>$getData['room_id'],'status'=>'1'])->find();
            if(!empty($roomStatus)){
                $this->result('房间内有预订', '', 204);
            }
            $getData['create_time'] = time();
            $getData['store_id'] =  $this->userInfo['store_id'];
            $getData['create_id'] = $this->userInfo['id'];
            $insert = Db::name('qt_room_book')->insert($getData);
            if($insert){
                $this->result('添加成功', '', 200);
            }
            $this->result('添加失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 编辑保存订房
     */
    public function save(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            $validate = new Validate();
            if (!$validate->scene('save')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            unset($getData['create_time']);
            $update= Db::name('qt_room_book')->where('id',$getData['id'])->update($getData);
            if($update !== false){
                $this->result('修改成功', '', 200);
            }
            $this->result('修改失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }




    /**
     * 取消订房
     */
    public function delete(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            Db::name('qt_room_book')->where('id',$getData['id'])->update(['status'=>'2']);
            $this->result('删除成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 确认到达
     */
    public function confirm(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $update['status'] = '3';
            if(isset($getData['people_num']) || !empty($getData['people_num'])){
                $update['people_num'] = $getData['people_num'];
            }
            Db::name('qt_room_book')->where('id',$getData['id'])->update($update);
            $this->result('确认成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }



}
